//
//  GLLiveCoHostManager.swift
//  GLUZSwift
//
//  Created by xyanl on 2025/7/25.
//

import UIKit

class GLLiveCoHostManager: NSObject {
    
    static let shared = GLLiveCoHostManager()
    
    /// 连线人员列表
    var liveCoHoseList: [GLLiveCoHostModel] = []
    
    func removeAll() {
        liveCoHoseList = []
    }
    
    func removeCoHose(_ id: String) {
        if let u = liveCoHoseList.filter({ $0.id == id }).first {
            // 已经加入过
            liveCoHoseList.remove(u)
        }
    }
    
    func updateCoHose(_ id: String, isOn: Bool) {
        if let u = liveCoHoseList.filter({ $0.id == id }).first {
            u.isOn = isOn
        } else {
            liveCoHoseList.append(GLLiveCoHostModel(id: id, isOn: isOn))
        }
        
        liveCoHoseList.forEach { model in
            if model.id == id {
                model.isOn = isOn
            }
        }
    }
    
    
    /// 筛连麦主播,
    /// - Parameter list: 当前连麦成员的 id
    func filterCoHose(list: [String]) {
        xLog("音视频流不可用 某人断开连麦")
        for coHostModel in liveCoHoseList.reversed() {
            let list = list.filter({ $0 == coHostModel.id })
            // 找不到这个连麦主播,需要从列表中删除掉
            if list.isEmpty {
                xLog("移除连麦记录 id: \(coHostModel.id)")
                // 现有的连线列表中 已经没有 coHostModel 该成员了, 需要从记录中移除掉
                GLLiveCoHostManager.shared.liveCoHoseList.remove(coHostModel)
            }
        }
    }
    
}

class GLLiveCoHostModel: NSObject {
    var id: String
    var isOn: Bool
    init(id: String, isOn: Bool) {
        self.id = id
        self.isOn = isOn
    }
}
